package cn.darkfire.web.Filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter({"/sy4/index.jsp","/sy4/del.do","/sy4/edit.do","/sy4/edit.jsp","/sy4/user.jsp"})
public class AuthFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) servletRequest;
        HttpServletResponse httpRep = (HttpServletResponse) servletResponse;
        httpRep.setCharacterEncoding("utf-8");
        try{
            if(httpReq.getSession().getAttribute("user")==null){
                httpReq.getSession().setAttribute("error","未登录！");
                httpRep.sendRedirect("./error.jsp");
            }else{
                //放行
                filterChain.doFilter(servletRequest,servletResponse);
            }
        }catch (Exception e){
            e.printStackTrace();
            httpReq.getSession().setAttribute("error","异常：未登录！");
            httpRep.sendRedirect("./error.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
